package jmu.cze.controller;

import jmu.cze.pojo.Airport;
import jmu.cze.pojo.City;
import jmu.cze.pojo.Country;
import jmu.cze.pojo.Terminal;
import jmu.cze.service.LocationService;
import jmu.cze.utl.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
@RequestMapping("/admin")
public class LocationController {
    
    private LocationService locationService;

    @Resource(name = "locationServiceImpl")
    public void setLocationService(LocationService locationService) {
        this.locationService = locationService;
    }

    @GetMapping("/location")
    public String manageCountry(Model model){
        model.addAttribute("countryMap",locationService.selectAllCountries());
        model.addAttribute("cityMap",locationService.selectAllCities());
        model.addAttribute("airports",locationService.selectAllAirports());
        model.addAttribute("msg","Hi!");
        return "location";
    }

    @PostMapping("/addCountry")
    @ResponseBody
    public String addCountry(Country country){
        return ResultUtil.getResult(locationService.insertCountry(country));
    }

    @PostMapping("/updateCountry")
    @ResponseBody
    public String updateCountry(Country country){
        return ResultUtil.getResult(locationService.updateCountry(country));
    }

    @GetMapping("/deleteCountry/{id}")
    @ResponseBody
    public String deleteCountry(@PathVariable("id") int id){
        return ResultUtil.getResult(locationService.deleteCountryByID(id));
    }

    @PostMapping("/addCity")
    @ResponseBody
    public String addCity(City city){
        return ResultUtil.getResult(locationService.insertCity(city));
    }

    @PostMapping("/updateCity")
    @ResponseBody
    public String updateCity(City city){
        return ResultUtil.getResult(locationService.updateCity(city));
    }

    @GetMapping("/deleteCity/{id}")
    @ResponseBody
    public String deleteCity(@PathVariable("id") String id){
        return ResultUtil.getResult(locationService.deleteCityByID(id));
    }

    @PostMapping("/addAirport")
    @ResponseBody
    public String addAirport(Airport airport){
        return ResultUtil.getResult(locationService.insertAirport(airport));
    }

    @PostMapping("/updateAirport")
    @ResponseBody
    public String updateAirport(Airport airport){
        return ResultUtil.getResult(locationService.updateAirport(airport));
    }

    @GetMapping("/deleteAirport/{id}")
    @ResponseBody
    public String deleteAirport(@PathVariable("id") String id){
        return ResultUtil.getResult(locationService.deleteAirportByID(id));
    }

    @PostMapping("/addTerminal")
    @ResponseBody
    public String addTerminal(Terminal terminal){
        return ResultUtil.getResult(locationService.insertTerminal(terminal));
    }

    @PostMapping("/updateTerminal")
    @ResponseBody
    public String updateTerminal(Terminal terminal){
        return ResultUtil.getResult(locationService.updateTerminal(terminal));
    }

    @GetMapping("/deleteTerminal/{id}")
    @ResponseBody
    public String deleteTerminal(@PathVariable("id") int id){
        return ResultUtil.getResult(locationService.deleteTerminalByID(id));
    }
}
